import { Controller, Post, Body, UseGuards, ParseIntPipe, NotFoundException, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiProperty } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { QueryUsersDto } from './dto/query-users.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ApiResponse as ApiResponseDto } from '../common/dto/response.dto';

// 创建查询DTO
class GetUserDto {
  @ApiProperty({
    description: '用户ID',
    example: 1,
    required: false
  })
  id?: number;

  @ApiProperty({
    description: '用户名',
    example: 'admin',
    required: false
  })
  username?: string;
}

// 创建删除DTO
class DeleteUserDto {
  @ApiProperty({
    description: '用户ID',
    example: 1
  })
  id: number;
}

@ApiTags('用户管理')
@ApiBearerAuth('JWT-auth')
@Controller('users')
@UseGuards(JwtAuthGuard)
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post('create')
  @ApiOperation({
    summary: '创建用户',
    description: '创建新用户'
  })
  @ApiResponse({
    status: 200,
    description: '用户创建成功'
  })
  @ApiResponse({
    status: 409,
    description: '用户名或邮箱已存在'
  })
  async create(@Body() createUserDto: CreateUserDto) {
    const user = await this.usersService.create(createUserDto);
    return ApiResponseDto.success(user, '用户创建成功');
  }

  @Post('list')
  @ApiOperation({
    summary: '获取用户列表',
    description: '获取系统中所有用户列表（支持分页、搜索、筛选）'
  })
  @ApiResponse({
    status: 200,
    description: '获取用户列表成功'
  })
  async findAll() {
    const users = await this.usersService.findAll();
    return ApiResponseDto.success(users, '获取用户列表成功');
  }

  @Post('page')
  @ApiOperation({
    summary: '分页查询用户',
    description: '支持分页、搜索、筛选的用户查询'
  })
  @ApiResponse({
    status: 200,
    description: '分页查询成功'
  })
  async findWithPagination(@Body() queryDto: QueryUsersDto) {
    const result = await this.usersService.findWithPagination(queryDto);
    return ApiResponseDto.success(result, '分页查询成功');
  }

  @Post('detail')
  @ApiOperation({
    summary: '获取用户详情',
    description: '根据用户ID或用户名获取用户详细信息'
  })
  @ApiResponse({
    status: 200,
    description: '获取用户信息成功'
  })
  @ApiResponse({
    status: 404,
    description: '用户不存在'
  })
  async findOne(@Body() getUserDto: GetUserDto) {
    let user;
    if (getUserDto.id) {
      user = await this.usersService.findOne(getUserDto.id);
    } else if (getUserDto.username) {
      user = await this.usersService.findByUsername(getUserDto.username);
      if (!user) {
        throw new NotFoundException('用户不存在');
      }
    } else {
      throw new BadRequestException('请提供用户ID或用户名');
    }
    return ApiResponseDto.success(user, '获取用户信息成功');
  }

  @Post('update')
  @ApiOperation({
    summary: '更新用户',
    description: '更新用户信息'
  })
  @ApiResponse({
    status: 200,
    description: '用户更新成功'
  })
  @ApiResponse({
    status: 404,
    description: '用户不存在'
  })
  @ApiResponse({
    status: 409,
    description: '用户名或邮箱已存在'
  })
  async update(@Body() updateUserDto: UpdateUserDto & { id: number }) {
    const { id, ...updateData } = updateUserDto;
    const user = await this.usersService.update(id, updateData);
    return ApiResponseDto.success(user, '用户更新成功');
  }

  @Post('delete')
  @ApiOperation({
    summary: '删除用户',
    description: '删除指定用户'
  })
  @ApiResponse({
    status: 200,
    description: '用户删除成功'
  })
  @ApiResponse({
    status: 404,
    description: '用户不存在'
  })
  async remove(@Body() deleteUserDto: DeleteUserDto) {
    await this.usersService.remove(deleteUserDto.id);
    return ApiResponseDto.success(null, '用户删除成功');
  }
}
